//
// Created by Semir on 2020/3/27.
//

#include <iostream>

using namespace std;

int length;
string str[25];
int seqLen = 0;
int count[25] = {0};

void dfs(string s, int cLen) {
    seqLen = max(seqLen, cLen);
    for (int i = 0; i < length; i++) {
        int p = 1;
        bool flag = false;
        while (p < s.length()) {
            if (count[i] < 2 && s.substr(s.length() - p, p) == str[i].substr(0, p)) {
                flag = true;
                break;
            }
            p++;
        }
        if (flag) {
            count[i]++;
            dfs(str[i], cLen + str[i].length() - p);
            count[i]--;
        }
    }
}

int main() {
    cin >> length;
    for (int i = 0; i < length; i++) {
        cin >> str[i];
    }
    char start;
    cin >> start;
    for (int i = 0;  i < length; i++) {
        if (str[i][0] == start) {
            count[i]++;
            dfs(str[i], str[i].length());
            count[i]--;
        }
    }
    cout << seqLen << endl;
    return 0;
}